package
{
	import flash.display.MovieClip;

	public class TestDoorObject
	{
		public var name:String;
		public var vert:Number;
		public var doorI:Number;
		public var doorJ:Number;
		public var doorpos:Vector2;
		public var doorsize:Number;
		public var doorcell_N;
		public var doorcell_P;
		public var pos:Vector2;
		public var r:Number;
		public var deltaI:Number;
		public var deltaJ:Number;
		public var isOpen:Boolean;
		public var doortimer:Number;
		public var maxtimer:Number;
		public var isLocked:Boolean;
		public var isTrap:Boolean;
		public var openStateFront:Number;
		public var openStateBack:Number;
		public var mc:MovieClip;
		public var trigMC:MovieClip;

		public function TestDoorObject()
		{
			this.name = "door";
			this.vert = 0;
			this.doorI = 2;
			this.doorJ = 3;
			this.doorpos = new Vector2(29, 19);
			this.doorsize = 10;
			this.doorcell_N = 0;
			this.doorcell_P = 0;
			this.pos = new Vector2(32, 84);
			this.r = Main.getInstance().tiles.xw * 0.833333333333333;
			this.deltaI = 0;
			this.deltaJ = 0;
			this.isOpen = false;
			this.doortimer = 0;
			this.maxtimer = 5;
			this.isLocked = false;
			this.isTrap = false;
			this.openStateFront = TileMapCell.EID_OFF;
			this.openStateBack = TileMapCell.EID_OFF;
			Main.getInstance().objects.Register(this);
			this.mc = Main.getInstance().gfx.CreateSprite("debugTestDoorMC", NinjaGraphicsSystem.LAYER_WALLS);
			this.mc.visible = false;
			this.mc.gotoAndStop("closed_Trek");
			this.trigMC = Main.getInstance().gfx.CreateSprite("debugDoorTriggerMC", NinjaGraphicsSystem.LAYER_WALLS);
			this.trigMC.gotoAndStop("exit_closed");
			this.trigMC.visible = false;
		}

		public function Destruct():void
		{
			Main.getInstance().gfx.DestroyMC(this.mc);
			this.mc = null;
			Main.getInstance().gfx.DestroyMC(this.trigMC);
			this.trigMC = null;
		}

		public var openFrameLabel:String;
		public var closedFrameLabel:String;

		public function Init(params):void
		{
			var _local2 = params;
			if (_local2.length == 9)
			{
				this.deltaI = _local2[7];
				this.deltaJ = _local2[8];
				this.doorI = _local2[4] + this.deltaI;
				this.doorJ = _local2[5] + this.deltaJ;
				this.vert = _local2[2];
				this.isTrap = Boolean(_local2[3]);
				this.isLocked = Boolean(_local2[6]);
				this.doorcell_N = Main.getInstance().tiles.GetTile_I(this.doorI, this.doorJ);
				this.doorpos.x = this.doorcell_N.pos.x;
				this.doorpos.y = this.doorcell_N.pos.y;
				if (this.vert == 1)
				{
					this.doorpos.y = this.doorpos.y + this.doorcell_N.yw;
					this.doorsize = this.doorcell_N.xw;
					this.doorcell_P = this.doorcell_N.nD;
					this.openStateFront = this.doorcell_N.eD;
					this.openStateBack = this.doorcell_P.eU;
				}
				else
				{
					this.doorpos.x = this.doorpos.x + this.doorcell_N.xw;
					this.doorsize = this.doorcell_N.yw;
					this.doorcell_P = this.doorcell_N.nR;
					this.openStateFront = this.doorcell_N.eR;
					this.openStateBack = this.doorcell_P.eL;
				}
				if (this.isLocked)
				{
					this.openFrameLabel = "opening_Lock";
					this.closedFrameLabel = "closed_Lock";
					this.mc.gotoAndStop("closed_Lock");
					this.pos.x = _local2[0];
					this.pos.y = _local2[1];
					this.r = Main.getInstance().tiles.xw * 0.416666666666667;
					this.isTrap = false;
					this.isOpen = false;
					this.isLocked = true;
					this.trigMC.x = this.pos.x;
					this.trigMC.y = this.pos.y;
					this.trigMC.scaleX = (this.trigMC.scaleY = this.r * 1.5);
					this.trigMC.gotoAndStop("exit_closed");
					this.trigMC.visible = true;
				}
				else if (this.isTrap)
				{
					this.openFrameLabel = "open_Trap";
					this.closedFrameLabel = "closing_Trap";
					this.mc.gotoAndStop("open_Trap");
					this.pos.x = _local2[0];
					this.pos.y = _local2[1];
					this.r = Main.getInstance().tiles.xw * 0.416666666666667;
					this.isOpen = true;
					this.isLocked = false;
					this.isTrap = true;
					this.trigMC.x = this.pos.x;
					this.trigMC.y = this.pos.y;
					this.trigMC.scaleX = (this.trigMC.scaleY = this.r * 1);
					this.trigMC.gotoAndStop("exit_closed");
					this.trigMC.visible = true;
				}
				else
				{
					this.openFrameLabel = "opening_Trek";
					this.closedFrameLabel = "closing_Trek";
					this.pos.x = this.doorpos.x;
					this.pos.y = this.doorpos.y;
					this.r = Main.getInstance().tiles.xw * 0.833333333333333;
					this.isOpen = false;
					this.isLocked = false;
					this.isTrap = false;
					this.mc.gotoAndStop("closed_Trek");
				}
				Main.getInstance().objects.AddToGrid(this);
				Main.getInstance().objects.Moved(this);
				this.mc.scaleX = (this.mc.scaleY = 2 * this.doorcell_N.yw);
				this.mc.x = this.doorcell_N.pos.x;
				this.mc.y = this.doorcell_N.pos.y;
				if (this.vert == 1)
				{
					if (this.deltaJ == 0)
					{
						this.mc.rotation = 90;
						this.mc.y = this.mc.y - 1;
					}
					else
					{
						this.mc.y = this.mc.y + (this.doorcell_N.yw * 2);
						this.mc.rotation = 270;
					}
				}
				else if (this.deltaI == 0)
				{
					this.mc.rotation = 0;
					this.mc.x = this.mc.x - 1;
				}
				else
				{
					this.mc.x = this.mc.x + (this.doorcell_N.xw * 2);
					this.mc.rotation = 180;
				}
				this.mc.visible = true;
				this.UpdateEdges();
			}
		}

		public function UnInit():void
		{
			if (this.vert == 0)
			{
				this.doorcell_N.eR = this.openStateFront;
				this.doorcell_P.eL = this.openStateBack;
			}
			else
			{
				this.doorcell_N.eD = this.openStateFront;
				this.doorcell_P.eU = this.openStateBack;
			}
			Main.getInstance().objects.RemoveFromGrid(this);
			Main.getInstance().objects.EndUpdate(this);
		}

		public function DumpInitData():String
		{
			var _local2 = (((((((((((((((("" + this.pos.x) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + this.pos.y) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + this.vert) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + Number(this.isTrap)) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + (this.doorI - this.deltaI)) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + (this.doorJ - this.deltaJ)) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + Number(this.isLocked)) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + this.deltaI) + NinjaGame.OBJPARAM_SEPERATION_CHAR) + this.deltaJ;
			return(_local2);
			return(_local2);
		}

		public function UpdateEdges():void
		{
			if (this.vert == 0)
			{
				if (this.isOpen)
				{
					this.doorcell_N.eR = this.openStateFront;
					this.doorcell_P.eL = this.openStateBack;
				}
				else
				{
					this.doorcell_N.eR = TileMapCell.EID_SOLID;
					this.doorcell_P.eL = TileMapCell.EID_SOLID;
				}
			}
			else if (this.isOpen)
			{
				this.doorcell_N.eD = this.openStateFront;
				this.doorcell_P.eU = this.openStateBack;
			}
			else
			{
				this.doorcell_N.eD = TileMapCell.EID_SOLID;
				this.doorcell_P.eU = TileMapCell.EID_SOLID;
			}
		}

		public function Draw():void
		{
			if (this.isOpen)
			{
				this.mc.gotoAndPlay(this.openFrameLabel);
				this.trigMC.gotoAndStop("exit_open");
			}
			else
			{
				this.mc.gotoAndPlay(this.closedFrameLabel);
				this.trigMC.gotoAndStop("exit_closed");
			}
		}

		public function IdleAfterDeath():void
		{
			Main.getInstance().objects.RemoveFromGrid(this);
		}

		public function TestVsPlayer(guy):void
		{
			var p = guy.pos;
			var _local3 = this.pos.x - guy.pos.x;
			var _local2 = this.pos.y - guy.pos.y;
			if (Math.sqrt((_local3 * _local3) + (_local2 * _local2)) < (this.r + guy.r))
			{
				this.doortimer = 0;
				if (this.isTrap)
				{
					this.Close();
					Main.getInstance().objects.RemoveFromGrid(this);
					this.TestVsPlayer = null;
				}
				else if (!this.isOpen)
				{
					this.Open();
				}
			}
		}

		public function Open():void
		{
			this.isOpen = true;
			this.UpdateEdges();
			this.Draw();
			if ((!this.isTrap) && (!this.isLocked))
			{
				Main.getInstance().objects.StartUpdate(this);
			}
		}

		public function Close():void
		{
			Main.getInstance().objects.EndUpdate(this);
			this.isOpen = false;
			this.UpdateEdges();
			this.Draw();
		}

		public function Update():void
		{
			this.doortimer++;
			if (this.maxtimer < this.doortimer)
			{
				this.Close();
			}
		}
	}
}